console.log('===>', $event)
const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
const form144 = this.pageObj.getApiWidgetByAliasName('form_144')
const change = this.getApiPage().getApiWidgetByAliasName('change')?.widgetObj;
const prefix = this.pageObj.getVariables().find(v => v.name === "environmentType")?.value;
// 获取变量
var stream_url = this.getApiPage().getVariables().find(x => x.name == "stream_url").value;
const { stream_path, equipmentCode } = $event;
console.log(stream_url);

function basePost(url, body) {
  const headers = { "Content-Type": "application/json" };
  const option = { method: 'POST', headers, body: JSON.stringify(body) };
  return fetch(url, option).then(res => res.json());
}

if (widgets) {
  console.log(widgets);
  if ($event.host) {
    addPlayer({ ...$event, stream_path, equipmentCode, equipmentId: equipmentCode });
    // widgets.widgetObj.sullivan.addPlayerWidthData(`${stream_url}/api/v1/stream/rtc/play`, )

    const ptzCtrl = this.pageObj.getApiWidgetByAliasName('ptz-ctrl');
    const optss = this.pageObj.getApiWidgetsByClassName('optss');
    const optsf = this.pageObj.getApiWidgetsByClassName('opts-form');
    const ptzBg = this.pageObj.getApiWidgetByAliasName('ptz-background');
    const $form = this.pageObj.getApiWidgetByAliasName('form_14');
    const _form = this.pageObj.retrievePlugin('base-forms');
    console.log('$event: => ', ptzCtrl);
    if (ptzCtrl) {
      ptzCtrl.showWidget();
      ptzBg.showWidget();
      form144.hideWidget();
      optss.forEach(v => v.showWidget());
      optsf.forEach(v => v.showWidget());
      ptzCtrl.widgetObj.setPtzController({
        id: $event.deviceId,
        channel: $event.channelId
      })
    }

    change._option.defaultKey = 'preset';
    change.render();
    _form.setFormData('form_14', { id: equipmentCode });
    basePost(`/${prefix}IotDdevice/project-business-iot-service/hikvision/sdk/queryPresets`, { equipmentId: equipmentCode })
      .then(json => {
        if (json.success) {
          console.log('_form', _form);
          const list = json.result.map(v => ({
            ...v,
            sourceName: v.name,
            name: v.presetNum + '-' + v.name,
            value: v.presetNum
          }));
          $form.widgetObj._option.options = { list };
          this.pageObj.setVariables([{ name: 'device_list', value: list }]);
          $form.widgetObj.render();
          console.log('data', _form.getFormData('form_14'))
        } else {
          window.layer?.msg('获取预置位失败！');
          $form.widgetObj._option.options = { list: [] };
          $form.widgetObj.render()
        }
        _form.setFormData('form_14', { id: equipmentCode })
      })

  } else {
    window.layer?.msg('摄像头未找到配置！');
  }

} else {
  window.layer?.msg('摄像头未找到！');
}

function addPlayer(options) {
  widgets.widgetObj.sullivan.addPlayerWidthData(`${stream_url}/api/v1/stream/rtc/play`, options)
}

